<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class LogsPage {

	private $alerter;
	
	public function __construct() {
		$this->alerter = new Alerter();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		check_access("ADMIN");
	}

	function page_inc_execute_action($page, $action) {
		switch($action) {
			default:
				$this->alerter->add_alert("unknown action: $action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {

		$inputs = array();
		$creator = new FormCreator();
		$inputs[] = $creator->input_select("Specifiek zoeken naar", "search_target", array('%', 'gebruiker #', 'item #'), array('algemeen', 'gebruikersnummer', 'itemnummer'));
		$inputs[] = $creator->input_field("Zoeken naar", "search_term");
		$page->content['form'] = $creator->create("searchform", $inputs);
		
		if ($_POST['search_term']) {
			$where = "log LIKE " . db_sqlize("%{$_POST['search_target']}{$_POST['search_term']}%");
		}	

		$sql = db_query_select(array(array('*')), array('LOG'), $where, "log_date DESC");
		$sqlresult = db_do_query($page, $sql);
		
		$rows = array();
		while ($l = db_fetch_array($sqlresult)) {
			$rows[] = array(substr($l['log_date'],6,2) . "/" . substr($l['log_date'],4,2). "/" . substr($l['log_date'],0,4) . " " . substr($l['log_date'],8,2) . ":" . substr($l['log_date'],10,2), $l['ipstring'], $l['log']);
		}
		$headers = array("Datum", "IP", "actie");
		$widths = array(130, 170, 400);
		
		$page->content['table'] = array('headers' => $headers, 'rows' => $rows, 'widths' => $widths, 'aligns' => array());
		
	}


	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_form($page, $page->content['form']);
		print_table($page->content['table']);
	}
}

?>